﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Thaire.Investment.Common
{
    public class Calculator
    {
        public static int MonthCalendarDifference(DateTime stDate, DateTime endDate)
        {
            return Math.Abs((stDate.Month - endDate.Month) + 12 * (stDate.Year - endDate.Year));
        }

        public static double IRRPerDay(decimal purchaseValue, decimal faceValue, decimal yield, DateTime maturityDate, DateTime purchaseDate)
        {
            double[] param = new double[2];
            param[0] = (double)-purchaseValue;
            param[1] = (double)faceValue;
            double irrValue = Microsoft.VisualBasic.Financial.IRR(ref param, (double)yield);
            double dayDiff = maturityDate.Subtract(purchaseDate).TotalDays;

            // return Math.Round(Math.Exp(Math.Log(1 + Math.Round(irrValue, 5, MidpointRounding.AwayFromZero) / 100d) / (dayDiff)) - 1d, 7, MidpointRounding.AwayFromZero);
            return Math.Round(Math.Exp(Math.Log(1 + Math.Round(irrValue, 5, MidpointRounding.AwayFromZero)) / (dayDiff)) - 1d, 7, MidpointRounding.AwayFromZero);

        }

        public static double CalTime2Mature(DateTime maturityDate, DateTime sysDate)
        {
            var start = maturityDate;
            DateTime end = sysDate;
            if (start <= end)
                return -99;
            //find of diff month
            //   var diffMonths = (end.Month + end.Year * 12) - (start.Month + start.Year * 12) + 1;
            var diffMonths = (start.Month + start.Year * 12) - (end.Month + end.Year * 12);// +1;
            //find TIME2MATURE
            return (double)diffMonths / (double)12;
        }

        public static double? CalFixIncomeIssueTerm(DateTime? maturityDate, DateTime? issueDate)
        {

            if (maturityDate == null || issueDate == null)
                return null;

            double year = (maturityDate.GetValueOrDefault().Subtract(issueDate.GetValueOrDefault()).TotalDays / 365.00);

            return year;
            //คำนวณจาก Maturity Date - System Date จัดหน่วยเป็นปี ทศนิยม 2 ตำแหน่ง หาก < 0 ให้แสดงเป็น "N/A"

        }


        //public static int? CalPeriodYear(DateTime? startDate, DateTime? endDate)
        //{
        //    if (startDate.HasValue && endDate.HasValue)
        //    {
        //        return Convert.ToInt32(Math.Ceiling((decimal)endDate.GetValueOrDefault().Subtract(startDate.GetValueOrDefault()).TotalDays / (decimal)365));
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //    // roundup (LOANS_HEADER.CONTRACT_END_DATE - LOANS_HEADER.CONTRACT_START_DATE) / 365
        //}

        public static int? CountDiffMonthBetweenStartAndEndDate(DateTime? startDate, DateTime? endDate)
        {
            //if (startDate.HasValue && endDate.HasValue)
            //{
            //    return Convert.ToInt32(Math.Ceiling((decimal)endDate.GetValueOrDefault().Subtract(startDate.GetValueOrDefault()).TotalDays / (decimal)30));
            //}
            //else
            //{
            //    return null;
            //}
            ////  roundup (LOANS_HEADER.CONTRACT_END_DATE - LOANS_HEADER.CONTRACT_START_DATE) / 30
            if (startDate == null || endDate == null)
                return null;

            var start = startDate.Value;
            var end = endDate.Value;
            var dateSpan = DateTimeSpan.CompareDates(start, end);
            var y = dateSpan.Years;
            var m = dateSpan.Months + (y * 12);
            if (dateSpan.Days > 0)
                m++;
            return m;
        }
        public static decimal? CalPeriodYearAndMonth(DateTime? startDate, DateTime? endDate)
        {
            if (startDate == null || endDate == null)
                return null;

            var start = startDate.Value;
            var end = endDate.Value;
            var dateSpan = DateTimeSpan.CompareDates(start, end);
            var y = dateSpan.Years;
            var m = dateSpan.Months;
            if (dateSpan.Days > 0)
                m++;
            //return Convert.ToDecimal(string.Format("{0}.{1}", y, m));
            var totoal = (((Convert.ToDouble(y) * 12)) + Convert.ToDouble(m)) / 12;
            var result = Convert.ToDecimal(Math.Round(totoal, 2));
            return result;
        }
        public static decimal CalLoanInstallment(decimal contractAmt, decimal interestRate, int periodMonth)
        {
            //=(LOANS_HEADER.CONTRACT_AMOUNT + (LOANS_HEADER.CONTRACT_AMOUNT x LOANS_INTEREST.INTEREST_RATE)) / LOANS_AMORTIZE_DETAIL.PERIOD_CAL

            return (contractAmt + (contractAmt * (interestRate / (decimal)100))) / periodMonth;
        }
    }
}
